<?php

namespace App\Http\Controllers\Web;

use App\Models\Articles;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Db;

class NewsController extends Controller
{
    public $result = array("status"=>0,'msg'=>'请求成功','data'=>"");

    /**
     * 列表
     *
     * @return \Illuminate\Http\Response
     */
    public function news(Request $request)
    {
        if ($request->ajax()) {
        	
        	$sortName = $request->post("sortName");    //排序列名
			$sortOrder = $request->post("sortOrder");   //排序（desc，asc）
			$pageNumber = $request->post("pageNumber");  //当前页码
			$pageSize = $request->post("pageSize");   //一页显示的条数
			$start = ($pageNumber-1)*$pageSize;   //开始位置
			$search = $request->post("search",'');  //搜索条件
			
			$total = Articles::where(function ($query){
	                    $query->where('read_power','=',0)
	                    ->orwhere('read_power','=',2);
	     			})
	     			->where('typeid','=','1')
	     			->where('is_display','=','0');
			$rows = Articles::where(function ($query){
	                    $query->where('read_power','=',0)
	                    ->orwhere('read_power','=',2);
	     			})
	     			->where('typeid','=','1')
	     			->where('is_display','=','0');
			
	        if(trim($search)){
	        	$total->where(function ($query) use ($search) {
                    $query->where('title', 'LIKE', '%' . $search . '%');
                });
	        	$rows->where(function ($query) use ($search) {
                     $query->where('title', 'LIKE', '%' . $search . '%');
                });
	        }
	        
	        $data['total'] = $total->count();
	        $data['rows'] = $rows->skip($start)->take($pageSize)
					        ->orderBy($sortName, $sortOrder)
					        ->get();
	        
	        return response()->json($data);
        }
        return view('web.articles.news');
    }

    /**
     * 日志
     *
     */

    public function log(Request $request)
    {
        $data["data"] = Articles::where(function ($query){
                    $query->where('read_power','=',0)
                    ->orwhere('read_power','=',2);
     			})
     			->where('typeid','=',3)
     			->where('is_display','=',0)
     			->orderBy("created_at", "desc")
     			->limit(10)
     			->get();
	    return view('web.articles.log',$data);
    }

	/**
	* 加载跟多日志
	*
	* @return \Illuminate\Http\Response
	*/	
	public function loadLog($id){
		$id=(int)$id;
		
		$data = Articles::where(function ($query){
                    $query->where('read_power','=',0)
                    ->orwhere('read_power','=',2);
     			})
     			->where('typeid','=',3)
     			->where('is_display','=',0)
     			->orderBy("created_at", "desc")
     			->limit(10)
     			->get();
        
	    if($data!=''){
	    	
        	$this->result["data"] = $data;
	        
	        return response()->json($this->result);
	    }else{
	    	
	    	$this->result['status']=1;
	    	$this->result['msg']="请求失败";
	    	return response()->json($this->result);
	    }
	}

    /**
     * 使用帮助
     *
     */
    public function help(Request $request)
    {
        $data = Articles::find((int)1);
        return view('web.articles.help',$data);
    }

    /**
     * 关于我们
     *
     */
    public function about(Request $request)
    {
        $data = Articles::find((int)2);
        return view('web.articles.about',$data);
    }

    /**
     * 使用帮助
     *
     */
    public function view($id)
    {
        $data = Articles::find((int)$id);
        return view('web.articles.view',$data);
    }


}
